<div class="headertitle">
    <label><h2>Profil</h2></label>
</div>
<?php
$prefix = "";
if (isset($_GET['ajax'])) {
    session_start();
    $prefix = "../";
}

include_once $prefix . 'controller/registrasi.php';
include_once $prefix . 'controller/login.php';
include_once $prefix . 'controller/follow.php';
include_once $prefix . 'controller/admin.php';
?>
<?php
if (isset($_GET['name_viewed'])!= NULL) {
    $name_viewed = $_GET['name_viewed'];
}
else if (isset($_SESSION['user_name'])!= NULL) {
    $name_viewed = $_SESSION['user_name'];
}
if (isset($_SESSION['user_name'])!= NULL) {
    $name_logon = $_SESSION['user_name'];
    $temp = new login();
    $role = $temp->getRole($name_logon );
}
else {
    $name_logon = NULL;
    $role = NULL;
}
?>
<?php
if (isset($_GET['fol'])!= NULL) {
    $doFoll = new follow();
    if($_GET['fol'] == 'yes') {
        $doFoll->addFollowing($name_logon, $name_viewed);
    }
    else if ($_GET['fol'] == 'no') {
        $doFoll->deleteFollowing($name_logon, $name_viewed);
    }
}
if(isset ($_POST['submitButton'])) {
// asumsikan semua sudah dicek pakai js
    $lontong = new registrasi();
    $lontong->register($_POST['username'], $_POST['nama'], $_POST['password'], $_POST['tanggalLahir'], $_POST['lokasi'], $_POST['jenisKelamin'], $_POST['email'], $_FILES['fotoProfil'],$_POST['wallprivacy']);
}

else if (isset ($_POST['submitButtonLogin'])) {
    $lontong = new login();
    $lontong->login($_POST['usernameLogin'], $_POST['passwordLogin']);
}
else if (isset ($_POST['submitButtonEditProfile'])) {
    $updateProf = new registrasi();
    $updateProf->updateProfile('1', $_SESSION['user_name'], $_POST['nama']);
    $updateProf->updateProfile('3', $_SESSION['user_name'], $_POST['tanggalLahir']);
    $updateProf->updateProfile('4', $_SESSION['user_name'], $_POST['lokasi']);
    $updateProf->updateProfile('5', $_SESSION['user_name'], $_POST['jenisKelamin']);
    $updateProf->updateProfile('6', $_SESSION['user_name'], $_POST['email']);
    $updateProf->updateProfile('8', $_SESSION['user_name'], $_POST['wallprivacy']);
}
else if (isset ($_POST['submitButtonEditPassword'])) {
    $updateProf = new registrasi();
    $updateProf->updateProfile('2', $_SESSION['user_name'], $_POST['password']);
}

if(isset ($_GET['newPhoto'])) {
    $changePhoto= new registrasi();
    $changePhoto->updateProfPict($name_logon,$_GET['newPhoto']);
}

if(isset($_GET['block'])) {
    if($_GET['block'] == 'blockdong') {
        $block = new admin();
        $block->blockUser($name_viewed);
    }
    else {
        $block = new admin();
        $block->unBlockUser($name_viewed);
    }
}
if(isset ($_GET['resetPass'])) {
    if($_GET['resetPass'] == 'yes') {
        $resPass = new admin();
        $resPass->resetPassword($name_viewed);
    }
}

$sapi = new login();
$tes = $sapi->showProfileByUserName($name_viewed);

while($row = mysql_fetch_array($tes)) {
    $array['user_name'] = $row['user_name'];
    $array['full_name'] = $row['full_name'];
    $array['birthday'] = $row['birthday'];
    $array['email'] = $row['email'];
    $array['gender'] = $row['gender'];
    $array['location'] = $row['location'];
    $array['url_photo'] = $row['url_photo'];
}
?>
<div class="isipagekiri">
    <div>
        <img
            src="<?php
            echo $array['url_photo'];
            ?>"
            alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100"
            />
    </div>
    <div>
        <?php
        if($name_logon == $name_viewed) {
            echo'<a href="gantiPhoto.php?name_viewed='.$name_logon.'">Ganti Photo</a>';

        }

        ?>
    </div>
    <div>
        <?php
        if($role != NULL) {
            $cekFollow = new follow();

            if ($name_logon == $name_viewed) {
                // profil sendiri
            }
            else if ($cekFollow->isFollowing($name_viewed, $name_logon))
            // jika sudah difollow
            {
                echo'<a href="profil.php?name_viewed='.$name_viewed.'&fol=no"><img src="images/unfollow1.png" alt="followbutton" height="30" width="100" border="0"/></a>';
            }
            else    // jika belum di follow
            {
                echo'<a href="profil.php?name_viewed='.$name_viewed.'&fol=yes"><img src="images/follow1.png" alt="followbutton" height="30" width="100" border="0"/></a>';
            }

        }
        /*
                    else if ($cekFollow->isFollowing($name_viewed, $name_logon))
                    // jika sudah difollow
                    {
                        echo'<a href="profil.php?name_viewed='.$name_viewed.'&fol=no"><img src="images/unfollow1.png" alt="followbutton" height="30" width="100" border="0"/></a>';
                    }
                    else    // jika belum di follow
                    {
                        echo'<a href="profil.php?name_viewed='.$name_viewed.'&fol=yes"><img src="images/follow1.png" alt="followbutton" height="30" width="100" border="0"/></a>';
                    }*/
        ?>
    </div>
    <div>
        <?php
        if ($role == 'admin' && $name_viewed !=$name_logon && $sapi->getAccessLogin($name_viewed) == 'yes') {
            echo'<a href="profil.php?name_viewed='.$name_viewed.'&block=blockdong"><img alt="block" src="images/block.png"/></a>';
        }
        else if($role == 'admin' && $name_viewed !=$name_logon && $sapi->getAccessLogin($name_viewed) == 'no') {
            echo'<a href="profil.php?name_viewed='.$name_viewed.'&block=unblockdong"><img alt="block" src="images/unblock.png"/></a>';
        }
//                    else if ($role != 'admin') {
//
//                    }
        ?>
    </div>
</div>
<div class="isipagetengah">
    <div>
        <label id="_name">Nama</label>
        <label id="name">
            <?php
            echo $array['full_name'];
            ?>
        </label>
    </div>
    <div>
        <?php
        if ($role == 'admin' && $name_viewed !=$name_logon) {
            echo'<a href="profil.php?name_viewed='.$name_viewed.'&resetPass=yes"><img alt="Reset Password" src="images/resetpass.png"/></a>';
        }
        ?>
    </div>
    <div>
        <label id="_tanggal_lahir">Tanggal Lahir</label>
        <label id="tanggal_lahir">
            <?php
            echo $array['birthday'];
            ?>
        </label>
    </div>
    <div>
        <label id="_email">Email</label>
        <label id="email">
            <?php
            echo $array['email'];
            ?>
        </label>
    </div>
    <div>
        <label id="jenis_kelamin">Jenis Kelamin</label>
        <label id="_jenis_kelamin">
            <?php
            echo $array['gender'];
            ?>
        </label>
    </div>
    <div>
        <label id="_lokasi">Lokasi</label>
        <label id="lokasi">
            <?php
            echo $array['location'];
            ?>
        </label>
    </div>

    <?php
    if ($name_logon == $name_viewed) {
        echo'<div><a href="edit_profile.php">Edit my profile</a></div>';
    }
    ?>
    <div>
        <?php
        echo'<a href="wall.php?name_viewed='.$name_viewed.'">Walls</a>';
        ?>
        |
        <?php
        echo'<a href="foto_album.php?name_viewed='.$name_viewed.'">Photos</a>';
        ?>
        <?php
        if($name_logon != $name_viewed && $role != NULL) {
            echo '|';
            echo'<a href="create_message.php?to='.$name_viewed.'">Sent Message</a>';
        }
        ?>
    </div>
</div>
<div class="isipagekanan">
    <div>
        <div class="followheight">
            <label>Following</label>
            <div>
                <?php
                $userFollow = new follow();
                $result = $userFollow->showFollowing($name_viewed);
                $iterate=0;
                while(($row = mysql_fetch_array($result))&&($iterate<5)) {
                    ?>
                <div class="floatleft" id="followgallery">
                        <?php
                        $iterate++;
                        $urlProfile = "profil.php?name_viewed=" . $row['user_name_following'];
                        ?>
                    <a href="<?php echo $urlProfile ?>" >
                        <img id="<?php echo "foto_" . $iterate?>" name="<?php echo "foto_" . $iterate?>" src="<?php echo $row['url_photo'] ?>" alt="<?php echo "foto_" . $iterate?>" width="40" height="40" border="0"/>
                    </a>
                    <label><?//php echo $row['user_name_following'] ?></label>
                </div>
                    <?php } ?>
            </div>
        </div>
        <div  class="followheight">
            <div><label>Follower</label></div>

            <div>
                <?php
                $result2 = $userFollow->showFollowed($name_viewed);
                $iterate=0;
                while(($row2 = mysql_fetch_array($result2))&&($iterate<5)) {
                    ?>
                <div class="floatleft" id="followgallery">
                        <?php
                        $iterate++;
                        $urlProfile = "profil.php?name_viewed=" . $row2['followed'];
                        ?>
                    <a href="<?php echo $urlProfile ?>" >
                        <img id="<?php echo "foto_" . $iterate?>" name="<?php echo "foto_" . $iterate?>" src="<?php echo $row2['url_photo'] ?>" alt="<?php echo "foto_" . $iterate?>" width="40" height="40" border="0"/>
                    </a>
                    <label><?//php echo $row['followed'] ?></label>
                </div>
                    <?php } ?>
            </div>
        </div>
    </div>
</div>